import json
import traceback

from app.health_manage.models import HealthLevel
from app.health_manage.other_models import AssetParamDict, CustomUserColumns
from app.health_manage.utils import dict_handle
from app.vecmdb import get_ci


def get_datacenter():
    payload = {
        "q": "_type:IDC",
        "count": 50,
        "page": 1,
    }
    res = get_ci(payload)
    if 'message' in res:
        return []
    room_list = []
    for item in res.get('result'):
        if {"label": item.get("serverRoomName"), "value": item.get("serverRoomName")} not in room_list:
            room_list.append({"label": item.get("serverRoomName"), "value": item.get("serverRoomName")})
    return room_list


def health_manage_columns():
    """健康度信息表头"""
    columns = [{
            "type": "selection",
            "prop": "id",
            "fixed": "left",
            "width": 35
        },
        {
            "label": "资产SN",
            "prop": "systemSN",
            "minWidth": 150,
            "fixed": "left",
        },
        {
            "label": "管理IP",
            "prop": "manageIp",
            "minWidth": 120,
        },
        {
            "label": "带外IP",
            "prop": "ipmi",
            "minWidth": 120,
        },
        {
            "label": "所在机房",
            "prop": "dataCenter",
            "minWidth": 110,
        },
        {
            "label": "所在位置",
            "prop": "position",
            "minWidth": 180,
            "fixed": "left",
        },
        {
            "label": "资产型号",
            "prop": "model",
            "minWidth": 120,
        },
        {
            "label": "资产厂商",
            "prop": "manu",
            "minWidth": 110,
        },
        {
            "label": "使用人",
            "prop": "userAccount",
            "minWidth": 110,
        },
        {
            "label": "使用人产线",
            "prop": "userProductLine",
            "minWidth": 120,
        },
        {
            "label": "维保开始日期",
            "prop": "warrantySince",
            "minWidth": 120,
        },
        {
            "label": "维保终止日期",
            "prop": "warrantyEnd",
            "minWidth": 120,
        },
        {
            "label": "到货日期",
            "prop": "arrivalDate",
            "minWidth": 120,
        },
        {
            "label": "健康分数",
            "prop": "healthScore",
            "minWidth": 120,
        },
        {
            "label": "计算时间",
            "prop": "calculateTime",
            "minWidth": 120,
        },
        {
            "label": "故障频率",
            "prop": "faultFrequency",
            "minWidth": 110,
        },
        {
            "label": "寿命期",
            "prop": "lifeTime",
            "minWidth": 110,
        },
        {
            "label": "专家经验",
            "prop": "expertise",
            "minWidth": 110,
        },
        {
            "label": "备注",
            "prop": "remarks",
            "minWidth": 110,
        },
        {
            "label": "锁定状态",
            "prop": "lockStatus",
            "minWidth": 90,
            "align": "center",
            "fixed": "right"
        },
        {
            "label": "健康度",
            "prop": "health",
            "minWidth": 90,
            "align": "center",
            "fixed": "right"
        }
    ]
    forms = [
        {
            "label": "健康度状态",
            "prop": "health",
            "type": "select",
            "viewType": "select",
            "bind": {
                "options": dict_handle(HealthLevel, "healthLevel", "default", filter_data=None),
                "placeholder": "请选择健康度状态"
            }
        },
        {
            "label": "锁定状态",
            "prop": "lockStatus",
            "type": "select",
            "viewType": "select",
            "bind": {
                "options": [{"label": "锁定", "value": "锁定"}, {"label": "未锁定", "value": "未锁定"}],
                "placeholder": "请选择状态"
            }
        },
        {
            "label": "管理IP",
            "prop": "manageIp",
            "type": "input",
            "bind": {
                "placeholder": "请输入管理IP"
            }
        },
        {
            "label": "带外IP",
            "prop": "ipmi",
            "type": "input",
            "bind": {
                "placeholder": "请输入带外IP"
            }
        },
        {
            "label": "所在机房",
            "prop": "dataCenter",
            "type": "select",
            "viewType": "select",
            "bind": {
                "options": get_datacenter(),
                "placeholder": "请选择所在机房"
            }
        },
        {
            "label": "使用人",
            "prop": "userAccount",
            "type": "input",
            "bind": {
                "placeholder": "请输入使用人"
            }
        },
        {
            "label": "使用人产线",
            "prop": "userProductLine",
            "type": "input",
            "bind": {
                "placeholder": "请输入产线"
            }
        },
        {
            "label": "厂商",
            "prop": "manu",
            "type": "select",
            "viewType": "select",
            "bind": {
                "options": dict_handle(AssetParamDict, "manufacturer", "server-db", filter_data={"big_category": "设备类"}),
                "placeholder": "请输入厂商"
            }
        },
        {
            "label": "型号",
            "prop": "model",
            "type": "select",
            "viewType": "select",
            "bind": {
                "options": dict_handle(AssetParamDict, "model", "server-db", filter_data={"big_category": "设备类"}),
                "placeholder": "请输入型号"
            }
        },
        {
            "label": "资产SN",
            "prop": "systemSN",
            "type": "input",
            "span": 2,
            "bind": {
                "type": "textarea",
                "maxlength": 1000,
                "autosize": {"minRows": 1, "maxRows": 4},
                "placeholder": "请输入资产SN,资产SN用英文逗号分割或换行分割"
            }
        }
    ]
    return columns, forms


def get_health_manage_columns_show(username):
    default_show = []
    columns, forms = health_manage_columns()
    for item in columns:
        default_show.append({"prop": item.get("prop"), "hide": False})
    info = CustomUserColumns.objects.using('server-db').filter(username=username, category='health-info').values().first()
    if not info:
        return default_show
    else:
        try:
            columns_show = json.loads(info['columns'])
            return columns_show
        except Exception as e:
            print(traceback.print_exc())
            return []


